package com.snail.billing.os;

import java.util.Stack;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;

import com.android.os.IPageManager;

public class DialogPageActivity extends Activity implements IPageManager {

	private Stack<DialogPage> stack = new Stack<DialogPage>();
	private DialogPage currentPage;
	private Bundle savedInstanceState;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.savedInstanceState = savedInstanceState;
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		if (currentPage != null) {
			currentPage.onRestart();
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		if (currentPage != null) {
			currentPage.onStart();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (currentPage != null) {
			currentPage.onResume();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (currentPage != null) {
			currentPage.onPause();
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (currentPage != null) {
			currentPage.onStop();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (currentPage != null) {
			currentPage.onDestroy();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (currentPage != null) {
			currentPage.onActivityResult(requestCode, resultCode, data);
		}
	}

	@Override
	public void onConfigurationChanged(Configuration config) {
		super.onConfigurationChanged(config);
		if (currentPage != null) {
			currentPage.onConfigurationChanged(config);
		}
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if (currentPage != null) {
			currentPage.onSaveInstanceState(outState);
		}
	}

	@Override
	public void onBackPressed() {
		backward();
	}

	@Override
	public void setFirstPage(Class<?> cls) {
		setFirstPage(cls, null);
	}

	@Override
	public void setFirstPage(Class<?> cls, Bundle args) {
		DialogPage newPage = null;
		try {
			newPage = (DialogPage) cls.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (newPage != null) {
			newPage.setPageManager(this);
			newPage.setPageArgs(args);
			setContentView(newPage.onCreateView(), newPage.getLayoutParams());
			newPage.onCreate(savedInstanceState);
			currentPage = newPage;
		}
	}

	@Override
	public void forward(Class<?> cls) {
		forward(cls, null);
	}

	@Override
	public void forward(Class<?> cls, Bundle args) {
		if (currentPage != null) {
			currentPage.onPause();
			currentPage.onStop();
			currentPage.onDestroy();
			stack.push(currentPage);
		}
		DialogPage newPage = null;
		try {
			newPage = (DialogPage) cls.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (newPage != null) {
			newPage.setPageManager(this);
			newPage.setPageArgs(args);
			setContentView(newPage.onCreateView(), newPage.getLayoutParams());
			newPage.onCreate(savedInstanceState);
			newPage.onStart();
			newPage.onResume();
			currentPage = newPage;
		}
	}

	@Override
	public void backward() {
		backward(0);
	}

	@Override
	public void backward(int count) {
		while (count > 0) {
			if (!stack.isEmpty()) {
				stack.pop();
			}
			count--;
		}
		if (!stack.isEmpty()) {
			if (currentPage != null) {
				currentPage.onPause();
				currentPage.onStop();
				currentPage.onDestroy();
			}
			DialogPage oldPage = stack.pop();
			setContentView(oldPage.onCreateView(), oldPage.getLayoutParams());
			oldPage.onCreate(savedInstanceState);
			oldPage.onRestart();
			oldPage.onStart();
			oldPage.onResume();
			currentPage = oldPage;
			return;
		}
		super.onBackPressed();
	}

}
